package pl.grabar.clinic.client.web.actions.doctor.patient;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import pl.grabar.clinic.core.dao.Dao;
import pl.grabar.clinic.core.domain.person.Patient;
import pl.grabar.clinic.core.domain.visit.Visit;
import pl.grabar.clinic.core.restful.RestfulClient;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

@Actions({@Action("previewOld"), @Action("preview")})
@InterceptorRef("grabarStack")
@Results({ @Result(name = "preview", location = "preview.jsp"), 
	@Result(name = "previewOld", location = "previewOld.jsp"),
	@Result(name = "input", type = "redirectAction", location = "visit/" , params={"namespace", "/doctor"}) })
public class LoadPatientRecord extends ActionSupport implements Preparable {

	private static final long serialVersionUID = 4370668290071781022L;

	private static final Log LOG = LogFactory.getLog(LoadPatientRecord.class);

	@Autowired
	private RestfulClient restfulClient;

	@Autowired
	private Dao<Patient> patientDao;
		
	private String patientPesel;

	private String patientId;

	private List<Visit> visits;

	private Patient existingPatient;
	
	private Patient patient;
	
	@Override
	public void prepare() throws Exception {
		if(StringUtils.isNoneBlank(patientPesel)){
	//		visits = restfulClient.getPatientRecordFromCentralBase(patientPesel);
			visits = restfulClient.getPatientRecordFromCentral(patientPesel);
		} else if (StringUtils.isNoneBlank(patientId)) {
			existingPatient = patientDao.findById(patientId);
			if (existingPatient != null) {
				visits = existingPatient.getPatientRecord().getVisits();
			}
		}
	}

	@Override
	public void validate() {
		if (!hasErrors()) {
			
			if(existingPatient == null &&  StringUtils.isNoneBlank(patientId)){
				addActionError(getText("pl.grabar.clinic.client.web.doctor.patient.actions.no-such-patient"));
				LOG.info("Pacjent o podanym id nie istnieje w bazie: " + patientId);
				return;
			} else {
				if (visits == null) {
					addActionError(getText("pl.grabar.clinic.client.web.doctor.patient.actions.empty-visit-list"));
					LOG.info("Lista wizyt jest null. Brak wizyt w bazie");
					return ;
				}
			}
			
			if (visits == null) {
				addActionError(getText("pl.grabar.clinic.client.web.doctor.patient.actions.connection-error"));
				LOG.info("Lista wizyt jest null. Wystąpił błąd połączenia z centralą");
				return ;
			}

			if (visits.size() == 0) {
				addActionError(getText("pl.grabar.clinic.client.web.doctor.patient.actions.empty-visit-list"));
				LOG.info("Lista wizyt pusta");
				return;
			}
		}
	}

	@Override
	public String execute() throws Exception {
		System.out.println(new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(visits));
		System.out.println(ActionContext.getContext().getName());
		patient = visits.iterator().next().getPatient();
		return ActionContext.getContext().getName();
	}

	/* AKCESORY */

	public String getPatientPesel() {
		return patientPesel;
	}

	public void setPatientPesel(String patientPesel) {
		this.patientPesel = patientPesel;
	}

	public String getPatientId() {
		return patientId;
	}

	public void setPatientId(String patientId) {
		this.patientId = patientId;
	}

	public List<Visit> getVisits() {
		return visits;
	}

	public void setVisits(List<Visit> visits) {
		this.visits = visits;
	}

	public void setRestfulClient(RestfulClient restfulClient) {
		this.restfulClient = restfulClient;
	}

	public void setPatientDao(Dao<Patient> patientDao) {
		this.patientDao = patientDao;
	}

	public Patient getPatient() {
		return patient;
	}

	public void setPatient(Patient patient) {
		this.patient = patient;
	}



}
